package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.Region;
import cn.wolfcode.luowowo.acticle.query.RegionQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IRegionService;
import cn.wolfcode.luowowo.common.exception.DisplayableException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/region")
public class RegionController {


    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model m, @ModelAttribute("qo") RegionQuery qo) {
        PageInfo query = regionService.query(qo);
        m.addAttribute("pageInfo", query);

        return "region/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        try {
            regionService.saveOrUpdate(region);
        } catch (DisplayableException e) {
            return new AjaxResult("亲,操作执行失败!");
        }
        return new AjaxResult("亲,操作执行成功!");
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(Long rid) {
        List<Destination> destByRegionId = destinationService.getDestByRegionId(rid);
        return destByRegionId;
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(Long id, boolean hot) {
        regionService.changeHotValue(id, hot);
        return new AjaxResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        regionService.delete(id);
        return new AjaxResult();
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(Integer deep) {
        List<Map<String, Object>> destByDeep = destinationService.getDestByDeep(deep);
        return destByDeep;
    }


}
